/**
 * Created by user on 2015/9/6.
 */
angular.module('backofficeApp.questionStoreManager.controllers', [])
    .controller('questionStoreCtrl', ['$scope','$http', '$location', function ($scope, $http, $location) {


        $scope.currentPage = 1;
        $scope.pageSize = 10;

        //帖库分页
        $scope.pagination = new pagination({currentPage:$scope.currentPage,pageSize: $scope.pageSize,
            url:'http://' + backOfficeHost + '/rest/v1.0/mis/questionStore/questionStoreList/:page/:size',
            ajax: $http,
            success:function(data){
                $scope.questionStoreList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;
                $scope.currentPage = $(".am-active").text();
                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $('#count').text("共"+$scope.pageNumber+"页 有"+$scope.count+"数据");
            },
            error: function(err){
                //$scope.communityList = null;
            },
            params: {}
        });

        //全选列表
        $scope.allSelected = function(){
            $scope.pagination.allSelected();
        };

        $scope.selectFile = function (file) {
            document.getElementById("image_result").innerHTML = '';
            showImages();
        };

        var reader = null;
        if (FileReader) {
            reader = new FileReader();
        } else {
            alert("浏览器暂时不支持FileReader");
        }
        var imageEle = document.getElementById("images_upload");
        var fileList = {}, length = 0, length1 = 0;

        var showImages = function () {

            //获取上传的多个文件
            fileList = imageEle.files;
            length = fileList.length;
            length1 = fileList.length;

            //获取文件信息
            readURL();
        };

        var readURL = function () {

            reader.readAsDataURL(fileList[length1 - length]);
            reader.onload = function () {
                var img = document.createElement("img");
                img.src = reader.result;
                img.width = 200;
                document.getElementById("image_result").appendChild(img);
                length--
                if (length == 0)return;
                readURL();
            };
        };



        //删除
        $scope.delete = function(){
            var params = {
                url: 'http://' + backOfficeHost + '/rest/v1.0/mis/questionStore/questionStoreDelete',
                checkObj: $(".item-check"),//checkbox对象
                key: "stroeId",
                success: function(){
                    var number = $(".am-active").text();//当前页码
                    $scope.pagination.selectPage(number);
                    alert("删除成功！");
                },
                error: function(){}
            };
            $scope.pagination.listDelete(params);
        };
        var fileList = {};
        //新增
        $scope.add = function(addData){

            if (fileList.length > 6) {
                alert('图片最多传6张');
                return;
            };

            var fd = new FormData();
            for(var key in addData) {
                if (addData[key]) fd.append(key, addData[key]);
            };
            if (fileList != null) {
                for (var i = 0; i < fileList.length; i++) {
                    var index = i + 1;
                    fd.append('image' + index, fileList[i]);
                }
            };

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/questionStore/questionStoreAdd', fd, {
                transformRequest: angular.identity, headers: {'Content-Type': undefined}
            }).success(function (data) {

                $scope.pagination.selectPage(1);//插入成功显示第一页
                $('#doc-modal-add').modal("close");
                alert("添加帖子成功！");

            }).error(function (err) {
                alert(err);
            });

        };
        //编辑
        $scope.update = function(updateData,index){

            var uData = {objId:"",questionStore:{}};
            if(updateData.objId)uData.objId = updateData.objId;
            if(updateData.storetype)uData.questionStore.storetype = updateData.storetype;
            if(updateData.category)uData.questionStore.category = updateData.category;
            if(updateData.desc)uData.questionStore.desc = updateData.desc;
            //uData = deleteObjNull(uData);
            var params = {
                url: 'http://' + backOfficeHost + '/rest/v1.0/mis/questionStore/questionStoreEdit',
                updateData: uData,
                success: function(data){
                    var number = $(".am-active").text();//当前页码
                    $scope.pagination.selectPage(number);

                    if(data.result.status == 1){
                        $('#doc-modal-'+(index)).modal("close");
                        alert("修改成功！");
                    }else {
                        $('#doc-modal-'+(index)).modal("close");
                        alert("修改失败！");
                    }
                },
                error: function(){
                    $('#doc-modal-'+(index)).modal("close");
                    alert("修改失败！");
                }
            };
            $scope.pagination.listUpdate(params);

        };


        //搜索
        $scope.search = function () {

            var opt ={
                desc : $scope.desc
            }

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/questionStore/questionStoreList/' + 1 + '/' + $scope.pageSize, opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.questionStoreList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;
                $scope.currentPage = $(".am-active").text();
                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $('#loading').modal("close");
            }).error(function (err) {
                alert(err);
                $('#loading').modal("close");
            });
        };


    }]).controller('questionPostCtrl', ['$scope','$http','$routeParams','$location', function ($scope, $http,$routeParams,$location) {


        $scope.currentPage = 1;
        $scope.pageSize = 10;

        //帖库分页
        $scope.pagination = new pagination({currentPage:$scope.currentPage,pageSize: $scope.pageSize,
            url:'http://' + backOfficeHost + '/rest/v1.0/mis/questionStore/questionStoreList/:page/:size',
            ajax: $http,
            success:function(data){
                $scope.questionStoreList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;
                $scope.currentPage = $(".am-active").text();
                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $('#count').text("共"+$scope.pageNumber+"页 有"+$scope.count+"数据");
            },
            error: function(err){
                //$scope.communityList = null;
            },
            params: {storetype:'op',model:2}
        });


        //搜索
        $scope.search = function () {

            var opt ={
                storetype:'op',
                desc : $scope.desc
            }

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/questionStore/questionStoreList/' + 1 + '/' + $scope.pageSize, opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.questionStoreList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;
                $scope.currentPage = $(".am-active").text();
                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $('#loading').modal("close");
            }).error(function (err) {
                alert(err);
                $('#loading').modal("close");
            });
        };



        //发布
        $scope.questionPost = function (question){

            var numbers =  $scope.postnumbers.split(',');
            /*  宝宝树66小区二次改造     mod  guo  20170825   start   */
           /* var opt ={
                question : question.objId,
                desc : question.desc,
                images : question.images,
                category : question.category,
                numbers: numbers
            }; 旧*/
             var opt ={
             question : question.objId,
             desc : question.desc,
             images : question.images,
             category : question.category,
             topFlg:'3',
             numbers: numbers
             };
            /*  宝宝树66小区二次改造     mod  guo  20170825   start   */
            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/questionStore/questionPost',opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                if (data.result.status == 1) {

                    //$('#delete-confirm-'+index).modal("close");
                    //$scope.searchReport();
                }else {
                    alert('发布失败');
                }
            }).error(function () {

            });


        };

        //级联框
        //var cascadData = {
        //    provinceUrl: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/region/regionPrivince',
        //    cityUrl: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/region/regionCity',
        //    cityAreaUrl: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/region/regionCounty',
        //    villageUrl: 'http://' + backOfficeHost + '/rest/v1.0/mis/community/communityList',
        //    ajax: $http
        //};
        //$scope.cascading = new gObj.cascading(cascadData);
        //$scope.cascading.paint($(".city-cascading")[0],true);

    }]);